//
//  SCIntegralMoreController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/30.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCIntegralMoreController.h"
#import "SCIntegralGoodsCell.h"
#import "SCGoodsDetailController.h"

@interface SCIntegralMoreController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray   *dataArray;

@end

@implementation SCIntegralMoreController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.lineView.hidden = NO;
    
    //设置collectionView
    [self setUpCollectionView];
    //请求数据
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self requestData];
}
- (void)setUpCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat w = (kScreenW-41)/3.0f;
    layout.itemSize = CGSizeMake(w, w*79/105+50);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0.5, kScreenW, kScreenH-kTopHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = BGColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:@"SCIntegralGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"SCIntegralGoodsCellID"];
    [self.view addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SCIntegralGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCIntegralGoodsCellID" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCModel *model = self.dataArray[indexPath.row];
    SCGoodsDetailController *vc = [[SCGoodsDetailController alloc]init];
    vc.goodsId = model.gift_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSDictionary *dict = @{@"token":Token,
                           @"size":@"15",
                           @"pagenow":@(1)};
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftApp&a=App_exchange"];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        //NSLog(@"---%@",result);
        [SVProgressHUD dismiss];
        NSArray *tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"data"]];
        [self.dataArray addObjectsFromArray:tempArray];
        [self.collectionView reloadData];
    }];
}


@end
